 var Model_ID = 0;
function MarkChange(n, t) {
  Model_ID = n;
  if ($("#mark_"+n).value() == "") {$("#model_"+n).html(""); $("#model_"+n).obj().disabled = true; return;}
  $.get("new/index.php", "0=ajax&1=get-models&no_sef=1&mark-id="+$("#mark_"+n).value(), MarkChange_Request);
  $("#model_"+n).obj().disabled = true;
  $("#model_"+n).html("");
  $("#mark_"+n).obj().disabled  = true;
  if (!t) {UpdateSearchRez();}
  return 0;
}
function MarkChange_Request(request) {
  if (!request.rez) {return 0;}
  var model  = $("#model_"+Model_ID).obj(); 
  model.disabled = false;
  $("#mark_"+Model_ID).obj().disabled = false; 
  model.innerHTML = request.options;
  UpdateSearchRez();
  return 0;
}
var carType = 0;
function ChangeCarType(obj) {
  var oldType = carType;
  if (obj.type == "radio") {
    if (obj.id == "selectCarType_c") {carType = 1;}
    if (obj.id == "allCarType_c") {carType = 0;}
  } else if (obj.type == "checkbox") {
    carType = 0;
    for (var i = 1; i <= 8; i++) {if ($("#categories_"+i).obj().checked) {carType = 1; break;}}
  }
  if (carType == oldType) {UpdateSearchRez(); return 0;}
  if (carType == 0) {
    for (var i = 1; i <= 8; i++) {$("#categories_"+i).obj().checked = false;}
    $("#selectCarType").css("font-weight", "100");
    $("#allCarType").css("font-weight", "700");
    $("#allCarType_c").obj().checked = true;
  } else {
    $("#allCarType").css("font-weight", "100");
    $("#selectCarType").css("font-weight", 700);
    $("#selectCarType_c").obj().checked = true;
  }
  UpdateSearchRez();
  return 0;
}
function UpdateSearchRez() {
  var form = $("#searchForm").obj(),
  request  = "0=ajax&1=search-count&no_sef=1";
  // категории
  for (var i = 1; i <= 8; i++) {
    var cat = $("#categories_"+i);
    if (cat.obj().checked && cat.value() != "") {request += "&categories[]="+cat.value();}
  }
  // марки
  for (var i = 1; i <= 3; i++) {
    var mark = $("#mark_"+i);
    if (mark.value() != "") {request += '&'+mark.obj().name+'='+mark.value();}
  }
  // модели
  for (var i = 1; i <= 3; i++) {
    var model = $("#model_"+i);
    if (model.value() != "") {request += '&'+model.obj().name+'='+model.value();}
  }
  // описание
  for (var i = 1; i <= 3; i++) {
    var name = $("#car-name_"+i);
    if (name.value() != "") {request += '&'+name.obj().name+'='+name.value();}
  }
  // тип топлива
  var fuel = $("#fuel").obj();
  for (var i = 0; i < fuel.children.length; i++) {if (fuel.children[i].selected && fuel.children[i].value != "") {request += '&'+fuel.name+'='+fuel.children[i].value;}}
  // тип топлива
  var gear = $("#transmission").obj();
  for (var i = 0; i < gear.children.length; i++) {if (gear.children[i].selected && gear.children[i].value != "") {request += '&'+gear.name+'='+gear.children[i].value;}}  
  // тип отделки
  var gear = $("#interiorTypes").obj();
  for (var i = 0; i < gear.children.length; i++) {if (gear.children[i].selected && gear.children[i].value != "") {request += '&'+gear.name+'='+gear.children[i].value;}}
  // цвет отделки
  var gear = $("#interiorColors").obj();
  for (var i = 0; i < gear.children.length; i++) {if (gear.children[i].selected && gear.children[i].value != "") {request += '&'+gear.name+'='+gear.children[i].value;}}
  // c повреждениями
  var NamesArr = ["#damaged", "#min-freg", "#max-freg", "#n_owners", "#max-mileage", "#min-mileage", "#min-power", "#max-power", "#minPrice", "#maxPrice", "#vatable", "#climatisation", "#numSeats", "#doorCount", "#emissionClass", "#safe-label", "#airbag"];
  for (var i = 0; i < NamesArr.length; i++) {
    var tmpObj = $(NamesArr[i]);
    if (tmpObj.value() != "") {request += '&'+tmpObj.obj().name+'='+tmpObj.value();}
  }
  //оппции салона
  for (var i = 1; i <= 19; i++) {
    var feature = $("#features_"+i);
    if (feature.obj().checked && feature.value() != "") {request += "&features[]="+feature.value();}
  }       
  //цвета
  for (var i = 1; i <= 14; i++) {
    var color = $("#colors_"+i);
    if (color.obj().checked && color.value() != "") {request += "&colors[]="+color.value();}
  }    
  // отправка
  request = request.replace(/(^\s+)|(\s+$)/g, "");
  $.get("new/index.php", request, UpdateSearchRez_Request);
  return 0;
}
function UpdateSearchRez_Request(request) {
  var search_count = $("#seach_count");
  if (!request.rez) {search_count.html("0");}
  else {search_count.html(request.search_count);}
  return 0;
}
function Form_Submit(id) {$("#"+id).obj().submit();}

var scroll_arr = [];
function Scroll_Down (id, i) {
  var obj = $("#"+id);
  if (scroll_arr[id] == 1) {
    i.innerHTML = "+";
    i.style["paddingLeft"]  = "5px";
    i.style["paddingRight"] = "5px";
    // скрытие открытого блока
    obj.roll_close(30, obj.obj().clientHeight, true);
    scroll_arr[id] = 0;
  } else {
    // анимация раскрытия
    i.innerHTML = "-";
    i.style["paddingLeft"]  = "8px";
    i.style["paddingRight"] = "7px";
    obj.show();
    obj.height("auto");
    obj.roll_open(60, obj.obj().clientHeight);
    scroll_arr[id] = 1;
  }
  return 0;
}